/**
 * jims
 */
package com.jims.master.modules.oper.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.oper.entity.ExamType;
import com.jims.master.modules.oper.entity.ExamVsClinic;
import com.jims.master.modules.oper.service.ClinicItemDictService;
import com.jims.master.modules.oper.service.ExamTypeService;
import com.jims.master.modules.oper.service.ExamVsClinicService;
import com.jims.master.modules.oper.utils.ExamTypeTreeTable;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 * @author 赵宁
 * @version 2017-02-23
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/examType")
public class ExamTypeController extends BaseController {

	@Autowired
	private ExamTypeService examTypeService;
	@Autowired
	private ExamVsClinicService examVsClinicService;
	@Autowired
	private ClinicItemDictService clinicItemDictService;
	@Autowired
	private OrgDeptService orgDeptService;

	@ModelAttribute
	public ExamType get(@RequestParam(required=false) String id) {
		ExamType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = examTypeService.get(id);
		}
		if (entity == null){
			entity = new ExamType();
		}
		return entity;
	}


	@RequiresPermissions("user")
	@RequestMapping(value = {"getPerformedBy", ""})
	@ResponseBody
	public StringData getPerformedBy(String id){
		StringData data=new StringData();
		ExamType examType = examTypeService.getPerformedBy(id);
		Map map = new HashMap();
		map.put("performedBy",examType.getPerformedBy());
		map.put("performedByName",examType.getRemarks());
		data.setDataMap(map);
		return data;
	}
	/**
	 * 加载列表
	 * @param examType
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String index(ExamType examType, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ExamType> list = examTypeService.findList(examType);
		List<ExamType> examTypeList = new ExamTypeTreeTable(list).buildTree();
		if(examTypeList.size()==0){
			examTypeList=list;
		}
		model.addAttribute("list", examTypeList);
		return "modules/oper/examTypeIndex";
	}

	/**
	 * 跳转到新增、修改页面
	 * @param examType
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExamType examType, Model model) {
		model.addAttribute("examType", examType);
		return "modules/oper/examTypeForm";
	}

	/**
	 * 保存/修改 方法
	 * @param examType
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExamType examType, Model model) {
		StringData data=new StringData();
		String orgId=UserUtils.getUser().getOrgId();
		examType.setOrgId(orgId);
		int i=examTypeService.save(examType);
		if(i>0){
			data.setCode("success");
			data.setData("保存检查项目类型成功");
		}else{
			data.setCode("error");
			data.setData("保存检查项目类型失败");
		}
		return data;
	}

	/**
	 * 删除方法
	 * @param examType
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExamType examType) {
		StringData data=new StringData();
        List<ExamType> list=examTypeService.findByPId(examType.getId());
        if(list.size()==0){
            int i=examTypeService.delete(examType);
            if(i>0){
                data.setCode("success");
                data.setData("删除检查项目类型成功");
            }else{
                data.setCode("error");
                data.setData("删除检查项目类型失败");
            }
        }else{
            data.setCode("error");
            data.setData("删除失败,请先删除子类后在删除父类");
        }

		return data;
	}
	/**
	 * 获取机构JSON数据。
	 * @param extId 排除的ID
	 * @param response
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		ExamType examType=new ExamType();
		User user= UserUtils.getUser();
		examType.setOrgId(user.getOrgId());
		List<ExamType> list = examTypeService.findList(examType);
		for (int i=0; i<list.size(); i++){
			ExamType e = list.get(i);
			if ((StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf("."+extId+".")==-1))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("pIds", e.getParentIds());
				map.put("name", e.getName());
				map.put("deptId",e.getPerformedBy());
				OrgDept orgDept=orgDeptService.get(e.getPerformedBy());
				map.put("deptName",orgDept!=null?orgDept.getDeptName():"");
				mapList.add(map);
			}
		}
		return mapList;
	}
	/**
	 * 加载检查项目页面
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"examItemList", ""})
	public String examItemList(String examTypeId,ExamVsClinic examVsClinic,HttpServletRequest request ,
							   HttpServletResponse response,Model model){
		if(examTypeId!=null && !"undefined".equals(examTypeId)){
			examVsClinic.setExamId(examTypeId);
		}
        List<ExamVsClinic>  examVsClinicList =examVsClinicService.findList(examVsClinic);
        model.addAttribute("list", examVsClinicList);
        model.addAttribute("examTypeId", examTypeId);
		return "modules/oper/examVsClinicIndex";
	}

    /**
     * 加载检查项目页面
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"examItemListRight", ""})
    public String examItemListRight(String examTypeId,ExamVsClinic examVsClinic,HttpServletRequest request ,
                               HttpServletResponse response,Model model){
        if(examTypeId!=null && !"undefined".equals(examTypeId)){
            examVsClinic.setExamId(examTypeId);
        }
        List<ExamVsClinic>  examVsClinicList =examVsClinicService.findList(examVsClinic);
        model.addAttribute("list", examVsClinicList);
        return "modules/oper/examVsClinicIndexRight";
    }

	/**
	 * 添加检查项目页面
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"examItemForm", ""})
    public String examItemForm(String id,Model model,ExamVsClinic examVsClinic,String performedBy,String deptName){
       if(id!=null && StringUtils.isNotBlank(id)){
		   examVsClinic.setId(id);
		   examVsClinic = examVsClinicService.get(examVsClinic);
	   }
        String examId=examVsClinic.getExamId();
        ExamType examType=examTypeService.get(examId);
        examVsClinic.setOrgDept(examType.getOrgDept());
        examVsClinic.setExamType(examType);
        model.addAttribute("examType",examType);
		model.addAttribute("examVsClinic",examVsClinic);
        return "modules/oper/examVsClinicForm";
	}

	/**
	 * 保存 检查项目对照数据
	 * @param examVsClinic
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"saveExamItem", ""})
	@ResponseBody
	public StringData saveExamItem(ExamVsClinic examVsClinic){
         StringData data = new StringData();
		 int i=examVsClinicService.save(examVsClinic);
		if(i>0){
			data.setCode("success");
			data.setData("保存数据成功");
		}else{
			data.setCode("error");
			data.setData("保存数据失败");
		}
		 return data;
	}

	/**
	 * 删除检查项目
	 * @param id
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "deleteExamItem")
	@ResponseBody
	public StringData deleteExamItem(String id ){
        ExamVsClinic examVsClinic = examVsClinicService.get(id);
		StringData data=new StringData();
		int i=examVsClinicService.delete(examVsClinic);
		if(i>0){
			data.setCode("success");
			data.setData("删除检查项目类型成功");
		}else{
			data.setCode("error");
			data.setData("删除检查项目类型失败");
		}
		return data;
	}
}